﻿Imports MySql.Data.MySqlClient

Public Class CMataPelajaran
    Inherits CContext

    Private INSERT_COMMAND As String
    Private UPDATE_COMMAND As String
    Private DELETE_COMMAND As String
    Private SELECT_COMMAND As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub simpan(ByVal nama As String)
        Me.INSERT_COMMAND = "INSERT INTO TBL_MATA_PELAJARAN (NAMA) VALUES ('" & nama & "')"
        MyBase.InsertData(INSERT_COMMAND)
    End Sub

    Public Sub rubah(ByVal nama As String, ByVal idMataPelajaran As Integer)
        Me.UPDATE_COMMAND = "UPDATE TBL_MATA_PELAJARAN SET NAMA='" & nama & "' WHERE ID=" & idMataPelajaran
        MyBase.UpdateData(UPDATE_COMMAND)
    End Sub

    Public Sub hapus(ByVal idMataPelajaran As Integer)
        'validasi
        If idMataPelajaran > 0 Then
            Me.DELETE_COMMAND = "DELETE FROM TBL_MATA_PELAJARAN WHERE ID=" & idMataPelajaran
            MyBase.DeleteData(DELETE_COMMAND)
        Else
            Throw New Exception
        End If
    End Sub

    Public Function getAll() As DataTable
        Me.SELECT_COMMAND = "SELECT * FROM TBL_MATA_PELAJARAN"
        Return MyBase.SelectData(SELECT_COMMAND)
    End Function

    Public Function getById(idmatapelajaran As Integer) As tbl_mata_pelajaran
        Dim _res As New tbl_mata_pelajaran
        MyBase.MySqlConnect.Open()
        Dim SELECT_CMD As MySqlCommand = MyBase.MySqlConnect.CreateCommand
        Dim reader As MySqlDataReader
        With SELECT_CMD
            .CommandText = "SELECT id,nama FROM TBL_mata_pelajaran  WHERE ID = " & idmatapelajaran
            reader = .ExecuteReader
        End With
        While reader.Read
            _res.id = reader.Item("id")
            _res.nama = reader.Item("nama")
            
        End While
        reader.Close()
        MySqlConnect.Close()
        Return _res
    End Function

End Class
